package com.ssm.helong.shop.service.impl;

import com.ssm.helong.shop.Dao.GoodsCategoryMapper;
import com.ssm.helong.shop.Dao.ShoppingCarDao;
import com.ssm.helong.shop.common.ServiceResultEnum;
import com.ssm.helong.shop.controller.vo.ShoppingCartItemVO;
import com.ssm.helong.shop.entity.ShoppingCar;
import com.ssm.helong.shop.service.ShoppingCarService;
import com.ssm.helong.shop.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {
    @Autowired
    private ShoppingCarDao shoppingCarDao;

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public String saveCartItem(ShoppingCar shoppingCar) {
        ShoppingCar temp = shoppingCarDao.selectByUserIdAndGoodsId(shoppingCar.getUserId(), shoppingCar.getGoodsId());
        // 判断是否这个用户已经将同样的商品存在购物车中
        // temp!=null=true
        // 存在的数量和新加入的数量相加
        if (temp != null) {
            temp.setGoodsCount(shoppingCar.getGoodsCount() + temp.getGoodsCount());
            return updateCartItem(temp);
        }
        //购物车不含这个用户id的当前商品
        if (shoppingCarDao.addShops(shoppingCar) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateCartItem(ShoppingCar shoppingCar) {
        shoppingCar.setUpdateTime(new Date());
        if(shoppingCarDao.updateByPrimaryKey(shoppingCar)>0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }


    @Override
    public Boolean deleteById(Integer cartItemId) {
        return shoppingCarDao.deleteByPrimaryKey(cartItemId)>0;
    }


    @Override
    public List<ShoppingCar> getMyShoppingCartItems(Integer userId, Integer pageNo, Integer pageSize) {

        List<ShoppingCar> shoppingCars=shoppingCarDao.findUserCar(userId);

        return shoppingCars;
    }
}
